<template>
	<div class="div-user-sum">
		<div class="div-user-top">
			<span>路由管理</span>
		</div>
		<el-button type="primary" style="margin:0 0 10px 10px" @click="openAddRouter(0)"  icon="el-icon-circle-plus-outline">添加根路由</el-button>
		<div style="margin: 10px;">
			<el-table :data="tableRouter" style="width: 100%" row-key="id" border :tree-props="treeProps">
				<el-table-column prop="text" label="路由名称">
				</el-table-column>
				<el-table-column label="操作" align="center">
					<template slot-scope="scope">
						<el-button type="primary" @click="openAddRouter(scope.row.id)" size="small">添加下级</el-button>
						<el-button type="primary" @click="openEditRouter(scope.row)" plain size="small">编辑</el-button>
						<el-button type="danger" @click="removeRouter(scope.row.id)" size="small">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<el-dialog title="添加路由" :show-close='false' :close-on-press-escape='false' :close-on-click-modal="false" :visible.sync="addRouterDialog" width="350px">
			<el-form :model="addForm" :rules="rules" ref="addForm">
				<el-form-item label="路由名称" prop="name">
					<el-input v-model="addForm.name" placeholder="请输入路由名称" clearable>
					</el-input>
				</el-form-item>
				<el-form-item label="权限标识" prop="perms">
					<el-input v-model="addForm.perms" placeholder="请输入权限标识，示例：sys:menu:menu" clearable>
					</el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="addRouterReset">取 消</el-button>
				<el-button type="primary" @click="toAddRouter" :disabled="dataloading">确 定</el-button>
			</div>
		</el-dialog>
		<el-dialog title="编辑路由" :show-close='false' :close-on-press-escape='false' :close-on-click-modal="false" :visible.sync="editRouterDialog" width="350px">
			<el-form :model="editForm" :rules="rules" ref="editForm">
				<el-form-item label="路由名称" prop="name">
					<el-input v-model="editForm.name" placeholder="请输入路由名称" clearable>
					</el-input>
				</el-form-item>
				<el-form-item label="权限标识" prop="perms">
					<el-input v-model="editForm.perms" placeholder="请输入权限标识，示例：sys:menu:menu" clearable>
					</el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="editRouterReset">取 消</el-button>
				<el-button type="primary" @click="editRouter">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
	import { routerList, routerGet, addRouter, updateRouter, deleteRouter } from '@/api/sysMenu.js'
	export default {
		name: 'Menu',
		data() {
			return {
				tableRouter: [],
				dataloading:false,
				addRouterDialog: false,
				editRouterDialog: false,
				treeProps: {
					children: 'children',
					hasChildren: null
				},
				addForm: {
					parentId: '',
					name: '',
					perms: '',
				},
				editForm: {
					menuId: '',
					name: '',
					perms: '',
				},
				rules: {
					name: [{
						required: true,
						message: '请输入路由名称'
					}],
					perms: [{
						required: true,
						message: '请输入权限标识'
					}]
				},
			}
		},
		created() {
			this.getRouterList();
		},
		methods: {
			getRouterList() {
				routerList({}).then(res => {
					this.tableRouter = res.children
				})
			},
			openAddRouter(parentId) {
				this.addRouterDialog = true;
				this.addForm.parentId = parentId;
			},
			toAddRouter() {
				this.dataloading = true;
				setTimeout(_ => {
					this.dataloading = false;
				}, 1000)
				this.$refs.addForm.validate((valid) => {
					if(valid) {
						addRouter({
							parentId: this.addForm.parentId,
							name: this.addForm.name,
							perms: this.addForm.perms,
						}).then(async res => {
							if(res.code === 'OK') {
								this.$message({
									message:'添加成功',
									type: 'success'
								});
								this.getRouterList();
								this.addRouterReset();
							} else {
								this.$message({
									message: res.msg,
									type: 'error'
								});
							}
						})
					}
				})
			},
			addRouterReset() {
				this.$refs.addForm.resetFields();
				this.$refs.addForm.clearValidate()
				this.addRouterDialog = false;
			},
			openEditRouter(item) {
				routerGet({
					id: item.id
				}).then(res => {
					this.editRouterDialog = true;
					this.editForm.menuId = item.id;
					this.editForm.name = res.data.name;
					this.editForm.perms = res.data.perms;
				})
			},
			editRouter() {
				this.$refs.editForm.validate((valid) => {
					if(valid) {
						updateRouter({
							name: this.editForm.name,
							perms: this.editForm.perms,
							menuId: this.editForm.menuId
						}).then(async res => {
							if(res.code === 'OK') {
								this.$message({
									message: '编辑成功',
									type: 'success'
								});
								this.getRouterList();
								this.editRouterReset();
							} else {
								this.$message({
									message: res.msg,
									type: 'error'
								});
							}
						})
					}
				})
			},
			editRouterReset() {
				this.editRouterDialog = false;
				this.$refs.editForm.resetFields();
				this.$refs.editForm.clearValidate()
			},
			removeRouter(mId) {
				this.$confirm('是否删除该路由及子路由?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					deleteRouter({
						mId: mId
					}).then(async res => {
						if(res.code === "OK") {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getRouterList();
						} else {
							this.$message({
								message: res.msg,
								type: 'error'
							});
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			}
		}
	}
</script>
<style lang="scss" scoped>
	.div-user-top {
		color: rgb(32, 29, 29);
		border-bottom: 1px solid rgba(32, 30, 30, 0.432);
		height: 70px;
		font-size: 20px;
		padding-top: 30px;
		padding-left: 50px;
		margin-bottom: 20px;
	}
</style>